package com.lancoo.lancooknowledgecourseware;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.lancoo.klgcourseware.base.BaseKlgActivity;
import com.lancoo.klgcourseware.entity.KlgApiResponse;
import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import com.lancoo.lancooknowledgecourseware.entity.ConditionData;
import com.lancoo.lancooknowledgecourseware.entity.KlgInformationModel;
import com.lancoo.lancooknowledgecourseware.widget.LoadingLayout;
import lancoo.com.net.retrofitrxjava.NetworkTransformer;
import lancoo.com.net.retrofitrxjava.network.Network;
import lancoo.com.net.retrofitrxjava.subscriber.NormalSubscriber;

public class KlgListActivity extends BaseKlgActivity implements OnLoadMoreListener, OnRefreshListener {

    protected String klgType;
    protected int pageIndex=1,pageTotalCount;
    private final int REFRESH=0;
    private final int LOADMORE=1;
    private final int PAGESIZE=20;
    private int status=REFRESH;
    private List<KlgInformationModel> klgInformationModelList=new ArrayList<>();
    protected SmartRefreshLayout refreshLayout;
    protected KlgListAdapter mAdapter;
    private LoadingLayout loadingLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_klg_list;
    }

    @Override
    protected String getTitleName() {
        return getIntent().getStringExtra("klgType");
    }

    @Override
    protected void setViewAndUpdata() {
        klgType = getIntent().getStringExtra("klgType");
        refreshLayout = findViewById(R.id.refreshLayout);
        loadingLayout = findViewById(R.id.loading_layout);
        loadingLayout.showContent();
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        mAdapter = new KlgListAdapter(this,klgInformationModelList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableFooterFollowWhenNoMoreData(false);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.autoRefresh();

    }


    private void requestInfo(){
        SharedPreferences sharedPreferences = getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE, Context.MODE_PRIVATE);
        String versionCode = sharedPreferences.getString(KlgDbConstant.SP_TRAIN_VERSIONCODE, "A");
//        if(TextUtils.equals("ESP",getIntent().getStringExtra("klgType"))){
//            versionCode="";
//        }
        if(TextUtils.isEmpty(versionCode)){
            klgType="ESP";
        }
        Network.getJsonData(VersionApi.class, MainActivity.BASE_URL).getKlgs("C",versionCode,klgType,pageIndex,PAGESIZE)
                .compose(NetworkTransformer.<KlgApiResponse<ConditionData>>commonSchedulers())
                .subscribe(new NormalSubscriber<KlgApiResponse<ConditionData>>(){
                    @Override
                    public void onNext(KlgApiResponse<ConditionData> conditionDataKlgApiResponse) {
                        super.onNext(conditionDataKlgApiResponse);
                        if(conditionDataKlgApiResponse.getState()==1){
                            pageTotalCount=(Integer.parseInt(conditionDataKlgApiResponse.getData().getKlgCount())-1)/PAGESIZE+1;
                            List<KlgInformationModel> klgInformatin = conditionDataKlgApiResponse.getData().getKlgInformatin();
                            if(status==REFRESH){
                                klgInformationModelList.clear();
                                refreshLayout.finishRefresh();
                            }
                            if(klgInformatin!=null&&klgInformatin.size()>0){
                                klgInformationModelList.addAll(klgInformatin);
                                refreshLayout.setEnableLoadMore(true);
                                loadingLayout.showContent();
                            }else{
                                loadingLayout.showEmpty();
                                refreshLayout.setEnableLoadMore(false);
                            }
                            if(isLoadMore()){
                                refreshLayout.finishLoadMore();
                                refreshLayout.resetNoMoreData();
                                Log.e("aaaaa","EnableLoadMore");
                            }else{
                                refreshLayout.finishLoadMoreWithNoMoreData();
                                Log.e("aaaaa","Non EnableLoadMore");
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if(refreshLayout!=null){
                            refreshLayout.finishRefresh();
                            refreshLayout.finishLoadMore();
                            refreshLayout.setEnableLoadMore(false);
                        }
                        if(loadingLayout!=null){
                            loadingLayout.showError();
                            loadingLayout.setErrorText("数据访问异常");
                            refreshLayout.findViewById(R.id.error_image).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    refreshLayout.autoRefresh();
                                }
                            });
                        }

                    }
                });
    }




    public void loadMoreData(){
        pageIndex++;
        status=LOADMORE;
        requestInfo();
    }


    public boolean isLoadMore(){
        return pageIndex<pageTotalCount;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (isLoadMore()) {
            loadMoreData();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageIndex=1;
        status=REFRESH;
        requestInfo();
    }
}
